package com.game.sample.impl;


import com.game.sample.Sample;
import com.game.sample.SampleFactory;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class SampleFactoryImpl<T extends Sample> implements SampleFactory<T> {
    private Map<Integer, Sample> sampleMap = new HashMap<>();

    public void addSample(T sample) {
        this.sampleMap.put(Integer.valueOf(((Sample)sample).sid), (Sample)sample);
    }

    public void addSamples(List<T> samples) {
        samples.forEach(sample -> addSample((T)sample));
    }

    public void reloadSamples(List<T> samples) {
        this.sampleMap.clear();
        addSamples(samples);
    }

    public T getSample(int sid) {
        return (T)this.sampleMap.get(Integer.valueOf(sid));
    }

    public T newSample(int sid) {
        T sample = getSample(sid);
        if (sample == null)
            return null;
        return (T)sample.clone();
    }

    public Collection<T> getAllSamples() {
        return (Collection)this.sampleMap.values();
    }
}